package org.gdpu.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gdpu.base.DaoSupport;
import org.gdpu.model.Groups;
import org.gdpu.model.Person;
import org.gdpu.service.PersonService;
import org.gdpu.service.TeamsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 下午1:01
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class PersonServiceImpl extends DaoSupport<Person> implements PersonService {

    private TeamsService teamsService;
    private final static Logger logger = LogManager.getLogger(TeamsServiceImpl.class);

    @Override
    public Person findPersonByAccout(String accout) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accout", accout);
        return (Person) this.findSpecialObject(Person.class, params);
    }

    @Override
    public String addStuToTeam(String info, String team) {

        info = getInfo(info);

        String[] studentInfo = info.split(";");

        StringBuffer stuNo = new StringBuffer();

        for (String stu : studentInfo) {
            if (this.findPersonByAccout(stu.split(",")[0]) != null) {
                stuNo.append(stu).append("<br/>");
                continue;
            }
            Person person = new Person();
            person.setAccout(stu.split(",")[0]);
            person.setEnable(true);
            person.setGroups(new Groups("学生"));
            person.setPassword(stu.split(",")[0]);
            person.setPersonName(stu.split(",")[1]);
            person.setTeams(teamsService.findByName(team));
            try {
                teamsService.save(person);
            } catch (Exception e) {
                logger.error(e);
                stuNo.append(person.getAccout()).append(",").append(person.getPersonName()).append("<br/>");
            }
        }

        return stuNo.toString();
    }

    @Resource(name = "teamsServiceImpl")
    public void setTeamsServer(TeamsService teamsServer) {
        this.teamsService = teamsServer;
    }

    private String getInfo(String name) {
        if (!name.contains(";") || !name.contains("；"))
            name += ";";
        name = name.replace("，", ",").replace("；", ";").replaceAll(" ", "");

        StringBuffer info = new StringBuffer();
        String regex = "[0-9]{10}(,|，)*[\\u4E00-\\u9FA5]{2,4}";
        Pattern pattern = Pattern.compile(regex);
        Matcher macher = pattern.matcher(name);
        while (macher.find()) {
            StringBuffer value = new StringBuffer();
            value.append(macher.group());
            if (!value.toString().contains(","))
                value.insert(10, ",");
            info.append(value).append(";");
        }
        return info.toString();
    }
}
